<?php
    include 'template/header.html';
require_once 'lib/function.php';

    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch ($action) {
        case 'list':

            $sql = "SELECT
customer.c_name,
relationship.r_id,
relationship.r_contime,
relationship.r_nextcontime,
relationship.r_contype,
relationship.r_contheme,
relationship.r_comment
FROM
customer
INNER JOIN relationship ON relationship.c_id = customer.c_id order by relationship.r_id";
             $rowNum = getRowNum($sql);
            $page = isset($_GET['page'])?$_GET['page']:1;
            $listNum = 6;
            $maxPage = ceil($rowNum/$listNum);
            $res = page($page,$sql, $listNum);
            // var_dump($res);
            include 'template/customer_relation_list.html';

            break;
        case 'add':
        $sql1 = "select c_id,c_name from customer";
         $cArr = getAllData($sql1);
            include 'template/customer_relation_add.html';
            break;
        case 'add_check':
            $theme = isset($_POST['theme'])?$_POST['theme']:'';
            $nexttime = isset($_POST['nexttime'])?$_POST['nexttime']:'';
            $r_obj = $_POST['r_obj']+0;
            $type = isset($_POST['type'])?$_POST['type']:'';
            $comment = isset($_POST['comment'])?$_POST['comment']:'';
            $date = date("Y-m-d H:i:s");
            // var_dump($_POST);
            $sql = "insert into relationship values('','$r_obj','$date','$nexttime','$type','$theme','$comment')";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('插入成功！');location.href='relation.php'</script>";
            }
            break;
        case 'del':
            $id = $_GET['id'];
            $sql = "delete from relationship where r_id='$id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('删除成功!');location.href='relation.php'</script>";
            }
            break;
        case 'update':
            $id = $_GET['id'];
            $sql1 = "select c_id,c_name from customer";
         $cArr = getAllData($sql1);
            $sql = "select * from relationship where r_id='$id'";
            $res = getAllData($sql);
            $res = $res[0];
            include 'template/customer_relation_add.html';
            break;
        case 'update_check':
            $id = $_GET['id'];
            $theme = isset($_POST['theme'])?$_POST['theme']:'';
            $nexttime = isset($_POST['nexttime'])?$_POST['nexttime']:'';
            $r_obj = $_POST['r_obj']+0;
            $type = isset($_POST['type'])?$_POST['type']:'';
            $comment = isset($_POST['comment'])?$_POST['comment']:'';
            $date = date("Y-m-d H:i:s");
            $sql = "update relationship set c_id='$r_obj',r_contime='$date',r_nextcontime='$nexttime',r_contype='$type',r_contheme='$theme',r_comment='$comment' where r_id='$id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('更新成功!');location.href='relation.php'</script>";
            }
            break;
    }
    include 'template/foot.html';